-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix autocompletion for LinkManager and AttributeManager #3985
Fix autocompletion for LinkManager and AttributeManager #3985
Conversation
I'm realising now that plugin developers might have made assumptions on the exception returned. I don't know if this is something we accept and we alert all plugin developers that in 1.3 this behaviour is changing, or there is something else we can do that is more backward-compatible. One option would be to create for now a combined class NotExistentAttributeError that inherits both from AttributeError and NotExistent? Would this be working and tenable also in the future when releasing a 2.0? |
Codecov Report
@@ Coverage Diff @@
## develop #3985 +/- ##
===========================================
+ Coverage 78.45% 78.50% +0.06%
===========================================
Files 461 461
Lines 34094 34077 -17
===========================================
+ Hits 26744 26750 +6
+ Misses 7350 7327 -23
Continue to review full report at Codecov.
|
I totally agree that raising However, I'm also convinced that we need a backwards-compatible solution for this. The
I'd favor adding a |
Indeed, this is my main concern... It would be great if there was a clever way to check if they are catching NotExistent to warn them. |
Yeah, it's a difficult one.. if we were to phase out the That also means there'd still be a valid use-case for catching |
Yeah. And actually, thinking better to it, indeed, we don't really need to deprecate the old behaviour. I just pushed the backwards-compatible fix to the code, and I think it's now OK (I tested and tab completion works) and shouldn't break backwards-compatibility |
Absolutely. It may have been misused in the managers, but I don't think we should get rid off it entirely. It is used in various methods where it is actually just fine. For example in the Couldn't we also make |
Sure, the plan was not to drop the
I think the current approach is better. It's backwards-compatible, and we don't risk that people catch a AttributeError and end up catching a NotExistent, instead. Also, we would need to subclass NotExistent also both from AttributeError and KeyError, that I don't think it's a great idea. For me the current code in the PR is a good final solution. |
I think the problem with subclassing I'm fine with the current solution, but would move the |
Ok, let's wait for another opinion before I change the code. It's a bit like now in Python3 you have a And actually it's ok if you catch the more specific exception (if you want to). No? |
Hmm, I think you're right - but it should definitely be carefully considered before we commit to this. As we've seen, exception types are kind of hard to change in a backwards-compatible way. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @giovannipizzi I am just wondering if we should still think about some deprecation path for the old NotExistent
raising behavior?
In the managers, exceptions were not caught properly. For instance, `getattr(calc.inputs, 'value')` was not return an AttributeError if `'value'` does not exist, and as a consequence `getattr(calc.inputs, 'value', None)` was raising instead of returning `None`. Similarly, I fixed `calc.inputs['value']` to raise a KeyError for not-existing `value`. This is now addressed by defining a new compound exception, that inherits both from NotExistent (for backwards-compatible reasons) and from the correct base exception of python (AttributeError or KeyError). The AttributeManager was not having Tab completion for a different reason, `__dir__` was returning tuples of dict items, rather than just the keys. Now these are fixed and tests are added (I cannot really test the TAB-completion functionality, but at least I test that the values of `dir()` and the exceptions raised are the correct ones). Fixes aiidateam#3984
e2d32c6
to
ee01669
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @giovannipizzi !
In the managers, exceptions were not caught properly.
For instance,
getattr(calc.inputs, 'value')
was not returnan AttributeError if
'value'
does not exist, and as aconsequence
getattr(calc.inputs, 'value', None)
was raisinginstead of returning
None
.Similarly, I fixed
calc.inputs['value']
to raise a KeyErrorfor not-existing
value
.The AttributeManager was not having Tab completion for a different
reason,
__dir__
was returning tuples of dict items, rather thanjust the keys.
Now these are fixed and tests are added (I cannot really test
the TAB-completion functionality, but at least I test that the
values of
dir()
and the exceptions raised are the correct ones).Fixes #3984